<?php
/**
 *@fileName Utils.php 工具类
 *@FileName Utils.php   工具通用函数类
 *@description 参考www.2tao.com/young 作者young jy620微信摇一摇红包工具类
 **/
class Utils {
	/**
	 * curl_post方法
	 *
	 */
	static function curl_post($url, $data) {
		$curl = curl_init($url);
		curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
		curl_setopt($curl, CURLOPT_TIMEOUT, 10);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($curl, CURLOPT_POST, 1);
		//发送一个常规的Post请求
		curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
		//Post提交的数据包
		$rv = curl_exec($curl);
		//输出内容
		curl_close($curl);
		return $rv;
	}

	/**
	 * curl_get方法
	 *
	 */
	static function curl_get($url) {
		$curl = curl_init($url);
		curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
		curl_setopt($curl, CURLOPT_TIMEOUT, 10);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
		$rv = curl_exec($curl);
		//输出内容
		curl_close($curl);

		/*$ch1 = curl_init();
		 $timeout = 5;
		 curl_setopt($ch1, CURLOPT_URL, $url_get);
		 curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
		 curl_setopt($ch1, CURLOPT_CONNECTTIMEOUT, $timeout);
		 curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, FALSE);
		 curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, false);
		 $accesstxt = curl_exec($ch1);
		 curl_close($ch1);
		 $access = json_decode($accesstxt, true);*/

		return $rv;
	}

	/**
	 *
	 *
	 * @param toURL
	 * @param paras
	 * @return
	 */
	function genAllUrl($toURL, $paras) {
		$allUrl = null;
		if (null == $toURL) {
			die("toURL is null");
		}
		if (strripos($toURL, "?") == "") {
			$allUrl = $toURL . "?" . $paras;
		} else {
			$allUrl = $toURL . "&" . $paras;
		}

		return $allUrl;
	}

	/**
	 *
	 *
	 * @param src
	 * @param token
	 * @return
	 */
	function splitParaStr($src, $token) {
		$resMap = array();
		$items = explode($token, $src);
		foreach ($items as $item) {
			$paraAndValue = explode("=", $item);
			if ($paraAndValue != "") {
				$resMap[$paraAndValue[0]] = $parameterValue[1];
			}
		}
		return $resMap;
	}

	/**
	 * trim
	 *
	 * @param value
	 * @return
	 */
	static function trimString($value) {
		$ret = null;
		if (null != $value) {
			$ret = $value;
			if (strlen($ret) == 0) {
				$ret = null;
			}
		}
		return $ret;
	}

	function formatQueryParaMap($paraMap, $urlencode) {
		$buff = "";
		ksort($paraMap);
		foreach ($paraMap as $k => $v) {
			if (null != $v && "null" != $v && "sign" != $k) {
				if ($urlencode) {
					$v = urlencode($v);
				}
				$buff .= $k . "=" . $v . "&";
			}
		}
		$reqPar;
		if (strlen($buff) > 0) {
			$reqPar = substr($buff, 0, strlen($buff) - 1);
		}
		return $reqPar;
	}

	function formatBizQueryParaMap($paraMap, $urlencode) {
		$buff = "";
		ksort($paraMap);
		foreach ($paraMap as $k => $v) {
			//	if (null != $v && "null" != $v && "sign" != $k) {
			if ($urlencode) {
				$v = urlencode($v);
			}
			$buff .= strtolower($k) . "=" . $v . "&";
			//}
		}
		$reqPar;
		if (strlen($buff) > 0) {
			$reqPar = substr($buff, 0, strlen($buff) - 1);
		}
		return $reqPar;
	}

	/**
	 * @description 获取随机字符串，默认长度为16
	 */
	static function random_str($length = 16) {
		$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		$str = "";
		for ($i = 0; $i < $length; $i++) {
			$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
			//$str .= $chars[ mt_rand(0, strlen($chars) - 1) ];
		}
		return $str;
	}

	/**
	 * @description 获取指定范围的随机数 ,默认为0到1之间
	 * @param {$min} 最小值
	 * @param {$max} 最大值
	 */
	static function random_int($min = 0, $max = 1) {
		srand((double)microtime() * 1000000);
		return rand($min, $max);
	}

	/**
	 * @description 获取指定位数的随机数
	 * @param {$length} 随机数长度，默认为4位
	 */
	static function generate_code($length = 4) {
		srand((double)microtime() * 1000000);
		return rand(pow(10, ($length - 1)), pow(10, $length) - 1);
	}

	/**
	 * @description 设置参数值
	 * @param {$key} 参数名称
	 * @param {$value} 参数值
	 * @param {$time} 保存期限
	 */
	static function set_session($key, $value, $time) {
		session_start();
		//使用SESSION前必须调用该函数。
		$_SESSION[$key] = $value;
		//注册一个SESSION变量
		$_SESSION['time'] = $time || time();
	}

	/**
	 * @description 获取session值
	 * @param {$key} 获取参数名称
	 */
	static function get_session($key) {
		if ($_SESSION) {
			return $_SESSION[$key];
		}
		return "";
	}

	static function arrayToXml($arr) {
		$xml = "<xml>";
		foreach ($arr as $key => $val) {
			if (is_numeric($val)) {
				$xml .= "<" . $key . ">" . $val . "</" . $key . ">";

			} else {
				$xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
			}
		}
		$xml .= "</xml>";
		return $xml;
	}

}
?>